<template>
  <view class="goods-item">
    <view class="left">
      <radio :checked="goods.goods_state" v-if="showRadio" color="#C00000" @click="radioClickHandler"></radio>
      <image :src="goods.goods_small_logo || defaultPic" mode="widthFix"></image>
    </view>
    <view class="right">
      <view class="goods-title">
        {{goods.goods_name}}
      </view>

      <view class="goods-info">
        <view class="goods-price">
          ￥{{goods.goods_price | tofixed}}
        </view>
        <uni-number-box v-if="showNum" @change="numChangeHandler" :min="1" :value="goods.goods_count">
        </uni-number-box>
      </view>

    </view>
  </view>
</template>

<script>
  export default {
    name: "my-goods",
    data() {
      return {
        defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png',
      };
    },
    methods: {
      radioClickHandler() {
        this.$emit('radio-change', {
          // 商品的 Id
          goods_id: this.goods.goods_id,
          // 商品最新的勾选状态
          goods_state: !this.goods.goods_state
        })
      },
      numChangeHandler(value) {
        console.log(value)
        this.$emit('num-change', {
          // 商品的 Id
          goods_id: this.goods.goods_id,
          // 商品最新的数量
          goods_count: value
        })
      }
    },
    //过滤器处理价格
    //使用管道符 | 调用过滤器
    filters: {
      tofixed(num) {
        //使用Number把num转成一个真正的数
        //然后使用toFixed保留小数位数
        return Number(num).toFixed(2);
      }
    },
    // mounted() {
    // },
    props: {
      goods: {
        type: Object,
        default: {}
      },
      showRadio: {
        type: Boolean,
        default: false
      },
      showNum: {
        type: Boolean,
        default: false
      }
    }
  }
</script>

<style lang="scss">
  .goods-item {
    display: flex;
    background-color: #fff;
    padding: 20rpx 10rpx;
    border-bottom: #efefef solid 5rpx;

    .left {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-right: 10rpx;

      radio {
        margin-right: 10rpx;
      }

      image {
        width: 250rpx;
        height: 250rpx;
      }
    }

    .right {
      display: flex;
      flex-wrap: wrap;
      align-content: space-between;


      .goods-title {
        margin: 0 10rpx;
        font-size: 24rpx;
        font-weight: bold;
      }

      .goods-info {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin: 10rpx 10rpx;
        width: 100%;

        .goods-price {
          color: #c00000;
          font-size: 38rpx;
        }
      }
    }
  }
</style>
